package com.example.demo.utils;

import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
import com.alibaba.druid.sql.ast.statement.SQLExprTableSource;
import com.alibaba.druid.sql.ast.statement.SQLSelect;
import com.alibaba.druid.sql.ast.statement.SQLSelectStatement;
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser;
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitorAdapter;
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlOutputVisitor;
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor;
import com.alibaba.druid.stat.TableStat;
import com.alibaba.druid.stat.TableStat.Condition;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.text.MessageFormat;
import java.util.List;

public class Sql {
    public static void main(String[] args) {  
        String tableName = "xx_comic_readrecord", column = "user_id";  
        String newTableFormater = "xx_comic_readrecord_{0}";  
        String sqlInsert = "insert into xx_comic_readrecord(user_id, comic_id, image_ids, chapter_ids) values(4, 891,?,?)";  
        String sqlSelect = "select user_id, comic_id, image_ids, chapter_ids from xx_comic_readrecord where user_id=4 and comic_id in (select comic_id from xx_comic where status=3)";  
        String sqlUpdate = "update xx_comic_readrecord set image_ids=? where user_id=4 and comic_id=891";  
        String sqlDelete = "delete from xx_comic_readrecord where user_id=4 and comic_id=891";  
        MySqlOutputVisitor output = new MySqlOutputVisitor(new StringBuilder());
          
        MySqlStatementParser parser = new MySqlStatementParser(sqlUpdate);
        MySqlSchemaStatVisitor schema = new MySqlSchemaStatVisitor();
        List<SQLStatement> sqlStatements = parser.parseStatementList();
        for(SQLStatement sqlStatement:sqlStatements){  
            sqlStatement.accept(schema);  
              
//          if(schema.containsTable(tableName)){  
//              TableNameModifier tnm = new TableNameModifier(tableName,"xx_comic_readrecord_4");  
//              sqlStatement.accept(tnm);  
//          }  
//            
              
            if(schema.getColumn(tableName, column)!=null){  
                for(Condition c:schema.getConditions()){
                    if(c.getColumn().getName().equals(column) && c.getOperator().equals("=")){  
                        Integer userId = (Integer) c.getValues().get(0);
                        System.out.println("userId:"+userId);
                        String newTableName = MessageFormat.format(newTableFormater, 5);
                        TableNameModifier tnm = new Sql().new TableNameModifier(tableName,newTableName);
                        sqlStatement.accept(tnm);  
                    }  
                }  
            }  
              
            sqlStatement.accept(output);  
        }  
          
        System.out.println(output.getAppender().toString().replaceAll("\n"," ").replaceAll("\t"," "));  
    }
    class TableNameModifier extends MySqlASTVisitorAdapter {
        private String tableName;
        private String newTableName;

        public TableNameModifier(String tableName, String newTableName) {
            this.tableName = tableName;
            this.newTableName = newTableName;
        }

        @Override
        public boolean visit(SQLSelectStatement astNode) {
            astNode.getSelect().accept(this);
            return false;
        }
        @Override
        public boolean visit(SQLSelect x){
            x.getQuery().accept(this);
            return false;
        }
        @Override
        public boolean visit(SQLExprTableSource x){
            x.getExpr().accept(this);
            return false;
        }
        @Override
        public boolean visit(SQLIdentifierExpr x) {
            if(x.getName().toLowerCase().trim().equals(tableName)){
                x.setName(newTableName);
            }
            try {
                System.out.println(new ObjectMapper().writeValueAsString(x.getAttributes()));
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
            return false;
        }
    }
}  